package by.pavlyukevich.paymentsystem.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.pavlyukevich.paymentsystem.dataaccess.filter.Filter;
import by.pavlyukevich.paymentsystem.dataaccess.impl.AbstractDaoImpl;
import by.pavlyukevich.paymentsystem.datamodel.AbstractEntity;
import by.pavlyukevich.paymentsystem.services.AbstractService;

public abstract class AbstractServiceImpl<ID, Entity extends AbstractEntity<ID>, T extends AbstractDaoImpl<ID, Entity>>
		implements AbstractService<ID, Entity> {

	protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractServiceImpl.class);

	@Inject
	public T dao;

	@PostConstruct
	private void init() {
		LOGGER.debug("Instance of " + getClass().getSimpleName() + " is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Entity get(ID id) {
		Entity entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Entity entity) {
		if (entity.getId() == null) {
			dao.insert(entity);
			LOGGER.debug("Save new: {}", entity);
		} else {
			dao.update(entity);
			LOGGER.debug("Update: {}", entity);
		}
	}

	@Override
	public void delete(Entity entity) {
		LOGGER.debug("Remove: {}", entity);
		dao.delete(entity.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all products");
		dao.deleteAll();
	}

	@Override
	public List<Entity> getList(Filter<Entity> filter) {
		return dao.getList(filter);
	}

	@Override
	public long getCount(Filter<Entity> filter) {
		return dao.getCount(filter);
	}

	@Override
	public List<Entity> getList() {
		return getList(null);
	}

	@Override
	public long getCount() {
		return getCount(null);
	}

}
